package morncloud.com.notes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import morncloud.com.notes.database.DAO;
import morncloud.com.notes.database.NoteDataUnit;
import morncloud.com.notes.ui.Utils;

public class EditNoteActivity extends Activity {
    private final String TAG = EditNoteActivity.class.getName();
    private int mStatus = 0;
    private final int EDIT_STATUS_CREATE = 0;
    private final int EDIT_STATUS_UPDATE = 1;
    private long mNoteId = 0;
    // define the edittext ,it will be seen as the workspace
    private EditText mNotes;
    private TextView mEditDate;
    private TextView mEditTime;
    private ImageView mBackSelector;
    private LinearLayout mBackContainer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_note);
        init();
    }

    /**
     * init the base view
     */
    private void init() {
        mNotes = (EditText) findViewById(R.id.edit_notes);
        mEditDate = (TextView) findViewById(R.id.edit_date);
        mEditTime = (TextView) findViewById(R.id.edit_time);
        mBackSelector = (ImageView) findViewById(R.id.edit_back_seletor);
        mBackContainer = (LinearLayout) findViewById(R.id.edit_back_container);
        View.OnClickListener listener = new BackColorChangedListener();
        findViewById(R.id.edit_color_0).setOnClickListener(listener);
        findViewById(R.id.edit_color_1).setOnClickListener(listener);
        findViewById(R.id.edit_color_2).setOnClickListener(listener);
        findViewById(R.id.edit_color_3).setOnClickListener(listener);
        findViewById(R.id.edit_color_4).setOnClickListener(listener);
        listener = new BackgroundShowListener();
        mBackSelector.setOnClickListener(listener);
        handleIntent();
    }

    private void handleIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            int code = intent.getIntExtra(Utils.NOTES_STATUS_KEY, Utils.NOTES_CREATE);
            if (code == Utils.NOTES_UPDATE) {
                mNoteId = intent.getLongExtra(Utils.NOTES_UPDATE_ID, 0l);
                mNotes.setText(DAO.getInstance(getBaseContext()).queryWithId(mNoteId).mData);
                mEditDate.setText("");
                mEditTime.setText("");
                mStatus = EDIT_STATUS_UPDATE;
            }
        }
    }

    private class BackColorChangedListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            int color = 0;
            switch (id) {
                case R.id.edit_color_0: {
                    color = R.drawable.edit_yellow;
                    break;
                }
                case R.id.edit_color_1: {
                    color = R.drawable.edit_blue;
                    break;
                }
                case R.id.edit_color_2: {
                    color = R.drawable.edit_white;
                    break;
                }
                case R.id.edit_color_3: {
                    color = R.drawable.edit_green;
                    break;
                }
                case R.id.edit_color_4: {
                    color = R.drawable.edit_red;
                    break;
                }
                default: {
                    color = R.drawable.edit_blue;
                    break;
                }
            }
            mBackContainer.setBackgroundResource(color);
            changeColorTag(View.GONE);
        }
    }

    private class BackgroundShowListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            changeColorTag(View.VISIBLE);
        }
    }

    private void changeColorTag(int status) {
        if (status == View.GONE | status == View.VISIBLE) {
            findViewById(R.id.edit_color_container).setVisibility(status);
            findViewById(R.id.edit_color_0).setVisibility(status);
            findViewById(R.id.edit_color_1).setVisibility(status);
            findViewById(R.id.edit_color_2).setVisibility(status);
            findViewById(R.id.edit_color_3).setVisibility(status);
            findViewById(R.id.edit_color_4).setVisibility(status);
        }
    }

    private void saveToDb() {
        Log.e("print", "saveToDb");
        String content = mNotes.getText().toString();
        if (content != null && !content.equals("")) {
            if (mStatus == EDIT_STATUS_CREATE) {
                DAO.getInstance(this).insertToNotes(content);
            } else if (mStatus == EDIT_STATUS_UPDATE) {
                NoteDataUnit unit = new NoteDataUnit();
                unit.mId = Long.toString(mNoteId);
                unit.mData = content;
                DAO.getInstance(this).updateWithId(unit);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        Log.e("Note",TAG+" onBackPressed");
        saveToDb();
        setResult(MainActivity.CODE_RELOAD_OK);
        super.onBackPressed();
    }
}
